{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "tensorboard_in_notebooks.ipynb",
      "version": "0.3.2",
      "provenance": [],
      "collapsed_sections": [],
      "toc_visible": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "metadata": {
        "id": "TsHV-7cpVkyK",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "##### Copyright 2019 The TensorFlow Authors."
      ]
    },
    {
      "metadata": {
        "id": "atWM-s8yVnfX",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
        "# you may not use this file except in compliance with the License.\n",
        "# You may obtain a copy of the License at\n",
        "#\n",
        "# https://www.apache.org/licenses/LICENSE-2.0\n",
        "#\n",
        "# Unless required by applicable law or agreed to in writing, software\n",
        "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
        "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
        "# See the License for the specific language governing permissions and\n",
        "# limitations under the License."
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "TB0wBWfcVqHz",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "# Using TensorBoard in Notebooks\n",
        "\n",
        "<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
        "  <td>\n",
        "    <a target=\"_blank\" href=\"https://www.tensorflow.org/tensorboard/tensorboard_in_notebooks\"><img src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" />View on TensorFlow.org</a>\n",
        "  </td>\n",
        "  <td>\n",
        "    <a target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/tensorboard/blob/master/docs/tensorboard_in_notebooks.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
        "  </td>\n",
        "  <td>\n",
        "    <a target=\"_blank\" href=\"https://github.com/tensorflow/tensorboard/blob/master/docs/tensorboard_in_notebooks.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a>\n",
        "  </td>\n",
        "</table>"
      ]
    },
    {
      "metadata": {
        "id": "elH58gbhWAmn",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "TensorBoard can be used directly within notebook experiences such as [Colab](https://colab.research.google.com/) and [Jupyter](https://jupyter.org/). This can be helpful for sharing results, integrating TensorBoard into existing workflows, and using TensorBoard without installing anything locally."
      ]
    },
    {
      "metadata": {
        "id": "VszJNloY3ZU3",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "## Setup"
      ]
    },
    {
      "metadata": {
        "id": "E6QhA_dp3eRq",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "Start by installing TF 2.0 and loading the TensorBoard notebook extension:\n",
        "\n",
        "**For Jupyter users:** If you’ve installed Jupyter and TensorBoard into\n",
        "the same virtualenv, then you should be good to go. If you’re using a\n",
        "more complicated setup, like a global Jupyter installation and kernels\n",
        "for different Conda/virtualenv environments, then you must ensure that\n",
        "the `tensorboard` binary is on your `PATH` inside the Jupyter notebook\n",
        "context. One way to do this is to modify the `kernel_spec` to prepend\n",
        "the environment’s `bin` directory to `PATH`, [as described here][1].\n",
        "\n",
        "[1]: https://github.com/ipython/ipykernel/issues/395#issuecomment-479787997\n"
      ]
    },
    {
      "metadata": {
        "id": "9w7Baxc8aCtJ",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "In case you are running a [Docker](https://docs.docker.com/install/) image of [Jupyter Notebook server using TensorFlow's nightly](https://www.tensorflow.org/install/docker#examples_using_cpu-only_images), it is necessary to expose not only the notebook's port, but the TensorBoard's port.\n",
        "\n",
        "Thus, run the container with the following command:\n",
        "\n",
        "```\n",
        "docker run -it -p 8888:8888 -p 6006:6006 \\\n",
        "tensorflow/tensorflow:nightly-py3-jupyter \n",
        "```\n",
        "\n",
        "where the `-p 6006` is the default port of TensorBoard. This will allocate a port for you to run one TensorBoard instance. To have concurrent instances, it is necessary to allocate more ports.\n"
      ]
    },
    {
      "metadata": {
        "id": "8p3Tbx8cWEFA",
        "colab_type": "code",
        "outputId": "508ab43d-1282-49a4-ba4e-a04f7778865f",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 85
        }
      },
      "cell_type": "code",
      "source": [
        "try:\n",
        "  # %tensorflow_version only exists in Colab.\n",
        "  %tensorflow_version 2.x\n",
        "except Exception:\n",
        "  pass\n",
        "\n",
        "# Load the TensorBoard notebook extension\n",
        "%load_ext tensorboard"
      ],
      "execution_count": 1,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "TensorFlow 2.x selected.\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "9GtR_cTTkf9G",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "Import TensorFlow, datetime, and os:"
      ]
    },
    {
      "metadata": {
        "id": "mVtYvbbIWRkV",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "import tensorflow as tf\n",
        "import datetime, os"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "Cu1fbH-S3oAX",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "## TensorBoard in notebooks"
      ]
    },
    {
      "metadata": {
        "id": "XfCa27_8kov6",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "Download the [FashionMNIST](https://github.com/zalandoresearch/fashion-mnist) dataset and scale it:"
      ]
    },
    {
      "metadata": {
        "id": "z8b82G7YksOS",
        "colab_type": "code",
        "outputId": "69f33eef-0951-4426-d77b-f208bac3d843",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 153
        }
      },
      "cell_type": "code",
      "source": [
        "fashion_mnist = tf.keras.datasets.fashion_mnist\n",
        "\n",
        "(x_train, y_train),(x_test, y_test) = fashion_mnist.load_data()\n",
        "x_train, x_test = x_train / 255.0, x_test / 255.0"
      ],
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz\n",
            "32768/29515 [=================================] - 0s 0us/step\n",
            "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-images-idx3-ubyte.gz\n",
            "26427392/26421880 [==============================] - 0s 0us/step\n",
            "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-labels-idx1-ubyte.gz\n",
            "8192/5148 [===============================================] - 0s 0us/step\n",
            "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-images-idx3-ubyte.gz\n",
            "4423680/4422102 [==============================] - 0s 0us/step\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "lBk1BqAZKEKd",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "Create a very simple model:"
      ]
    },
    {
      "metadata": {
        "id": "OS7qGYiMKGQl",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "def create_model():\n",
        "  return tf.keras.models.Sequential([\n",
        "    tf.keras.layers.Flatten(input_shape=(28, 28)),\n",
        "    tf.keras.layers.Dense(512, activation='relu'),\n",
        "    tf.keras.layers.Dropout(0.2),\n",
        "    tf.keras.layers.Dense(10, activation='softmax')\n",
        "  ])"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "RNaPPs5ZKNOV",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "Train the model using Keras and the TensorBoard callback:"
      ]
    },
    {
      "metadata": {
        "id": "lpUO9HqUKP6z",
        "colab_type": "code",
        "outputId": "40acf9dc-919d-4dca-8f83-fd11ac53f45c",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 204
        }
      },
      "cell_type": "code",
      "source": [
        "def train_model():\n",
        "  \n",
        "  model = create_model()\n",
        "  model.compile(optimizer='adam',\n",
        "                loss='sparse_categorical_crossentropy',\n",
        "                metrics=['accuracy'])\n",
        "\n",
        "  logdir = os.path.join(\"logs\", datetime.datetime.now().strftime(\"%Y%m%d-%H%M%S\"))\n",
        "  tensorboard_callback = tf.keras.callbacks.TensorBoard(logdir, histogram_freq=1)\n",
        "\n",
        "  model.fit(x=x_train, \n",
        "            y=y_train, \n",
        "            epochs=5, \n",
        "            validation_data=(x_test, y_test), \n",
        "            callbacks=[tensorboard_callback])\n",
        "\n",
        "train_model()"
      ],
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Train on 60000 samples, validate on 10000 samples\n",
            "Epoch 1/5\n",
            "60000/60000 [==============================] - 11s 182us/sample - loss: 0.4976 - accuracy: 0.8204 - val_loss: 0.4143 - val_accuracy: 0.8538\n",
            "Epoch 2/5\n",
            "60000/60000 [==============================] - 10s 174us/sample - loss: 0.3845 - accuracy: 0.8588 - val_loss: 0.3855 - val_accuracy: 0.8626\n",
            "Epoch 3/5\n",
            "60000/60000 [==============================] - 10s 175us/sample - loss: 0.3513 - accuracy: 0.8705 - val_loss: 0.3740 - val_accuracy: 0.8607\n",
            "Epoch 4/5\n",
            "60000/60000 [==============================] - 11s 177us/sample - loss: 0.3287 - accuracy: 0.8793 - val_loss: 0.3596 - val_accuracy: 0.8719\n",
            "Epoch 5/5\n",
            "60000/60000 [==============================] - 11s 178us/sample - loss: 0.3153 - accuracy: 0.8825 - val_loss: 0.3360 - val_accuracy: 0.8782\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "SxvXc4hoKW7d",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "Start TensorBoard within the notebook using [magics](https://ipython.readthedocs.io/en/stable/interactive/magics.html):"
      ]
    },
    {
      "metadata": {
        "id": "KBHp6M_zgjp4",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "%tensorboard --logdir logs"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "Po7rTfQswAMT",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "<img class=\"tfo-display-only-on-site\" src=\"https://github.com/tensorflow/tensorboard/blob/master/docs/images/notebook_tensorboard.png?raw=1\"/>"
      ]
    },
    {
      "metadata": {
        "id": "aQq3UHgmLBpC",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "You can now view dashboards such as scalars, graphs, histograms, and others. Some dashboards are not available yet in Colab (such as the profile plugin).\n",
        "\n",
        "The `%tensorboard` magic has exactly the same format as the TensorBoard command line invocation, but with a `%`-sign in front of it."
      ]
    },
    {
      "metadata": {
        "id": "NiIMwOG8MR_g",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "You can also start TensorBoard before training to monitor it in progress:"
      ]
    },
    {
      "metadata": {
        "id": "qyI5lrXoMw9K",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "%tensorboard --logdir logs"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "ALxC8BbWWV91",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "<img class=\"tfo-display-only-on-site\" src=\"https://github.com/tensorflow/tensorboard/blob/master/docs/images/notebook_tensorboard_two_runs.png?raw=1\"/>"
      ]
    },
    {
      "metadata": {
        "id": "GUSM8yLrO2yZ",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "The same TensorBoard backend is reused by issuing the same command. If a different logs directory was chosen, a new instance of TensorBoard would be opened. Ports are managed automatically. \n",
        "\n",
        "Start training a new model and watch TensorBoard update automatically every 30 seconds or refresh it with the button on the top right:"
      ]
    },
    {
      "metadata": {
        "id": "ixZlmtWhMyr4",
        "colab_type": "code",
        "outputId": "ed0662ab-9904-4ae8-9764-7b79b488ca76",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 204
        }
      },
      "cell_type": "code",
      "source": [
        "train_model()"
      ],
      "execution_count": 9,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Train on 60000 samples, validate on 10000 samples\n",
            "Epoch 1/5\n",
            "60000/60000 [==============================] - 11s 184us/sample - loss: 0.4968 - accuracy: 0.8223 - val_loss: 0.4216 - val_accuracy: 0.8481\n",
            "Epoch 2/5\n",
            "60000/60000 [==============================] - 11s 176us/sample - loss: 0.3847 - accuracy: 0.8587 - val_loss: 0.4056 - val_accuracy: 0.8545\n",
            "Epoch 3/5\n",
            "60000/60000 [==============================] - 11s 176us/sample - loss: 0.3495 - accuracy: 0.8727 - val_loss: 0.3600 - val_accuracy: 0.8700\n",
            "Epoch 4/5\n",
            "60000/60000 [==============================] - 11s 179us/sample - loss: 0.3282 - accuracy: 0.8795 - val_loss: 0.3636 - val_accuracy: 0.8694\n",
            "Epoch 5/5\n",
            "60000/60000 [==============================] - 11s 176us/sample - loss: 0.3115 - accuracy: 0.8839 - val_loss: 0.3438 - val_accuracy: 0.8764\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "IlDz2oXBgnZ9",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "You can use the `tensorboard.notebook` APIs for a bit more control:"
      ]
    },
    {
      "metadata": {
        "id": "ko9qeSQHLrEh",
        "colab_type": "code",
        "outputId": "909d2925-7dda-46a2-de5e-c6aba71973bc",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 51
        }
      },
      "cell_type": "code",
      "source": [
        "from tensorboard import notebook\n",
        "notebook.list() # View open TensorBoard instances"
      ],
      "execution_count": 10,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Known TensorBoard instances:\n",
            "  - port 6006: logdir logs (started 0:00:54 ago; pid 265)\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "hzm9DNVILxJe",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "# Control TensorBoard display. If no port is provided, \n",
        "# the most recently launched TensorBoard is used\n",
        "notebook.display(port=6006, height=1000) "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "za2GqzKiWY-R",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "<img class=\"tfo-display-only-on-site\" src=\"https://github.com/tensorflow/tensorboard/blob/master/docs/images/notebook_tensorboard_tall.png?raw=1\"/>"
      ]
    }
  ]
}